﻿Imports System.Net

Public Class FTPFunctions
    Private mFTPURL As String
    Private mUsername As String
    Private mPassword As String
    Sub New(FTPURL As String, Username As String, Password As String)
        mFTPURL = FTPURL
        mUsername = Username
        mPassword = Password
    End Sub
    Function CheckFTPDirectory() As String
        Dim request = DirectCast(WebRequest.Create(mFTPURL), FtpWebRequest)

        request.Credentials = New NetworkCredential(mUsername, mPassword)

        request.Method = WebRequestMethods.Ftp.ListDirectory

        Try
            Using response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
                Return ""
            End Using

        Catch ex As WebException
            Dim response As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
            'Does not exist
            Return "Error code: " & response.StatusCode
        End Try
    End Function
    Function CreateFTPDirectory() As String
        Dim request As Net.FtpWebRequest = CType(FtpWebRequest.Create(mFTPURL), FtpWebRequest)
        request.Credentials = New NetworkCredential(mUsername, mPassword)
        request.Method = WebRequestMethods.Ftp.MakeDirectory

        Try
            Using response As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
                Return ""
            End Using

        Catch ex As WebException
            Dim response As FtpWebResponse = DirectCast(ex.Response, FtpWebResponse)
            'Does not exist
            Return "Error code: " & response.StatusCode
        End Try
        Return True
    End Function
End Class
